#pragma once

#include <iostream>
#include <string>
#include <strings.h>
#include <cstring>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <functional>
#include <unordered_map>
#include "Log.hpp"

// using func_t = std::function<std::string(const std::string&)>;
typedef std::function<std::string(const std::string &, const std::string &, uint16_t)> func_t;

Log lg;

enum{
    SOCKET_ERR=1,
    BIND_ERR
};

uint16_t defaultport = 8080;
std::string defaultip = "0.0.0.0";
const int size = 1024;

class UdpServer{
public:
    UdpServer(const uint16_t &port = defaultport, const std::string &ip = defaultip):sockfd_(0), port_(port), ip_(ip),isrunning_(false)
    {}
    void Init()
    {
        // 1. 创建udp socket
        // 2. Udp 的socket是全双工的，允许被同时读写的
        sockfd_ = socket(AF_INET, SOCK_DGRAM, 0); // PF_INET
        if(sockfd_ < 0)
        {
            lg(Fatal, "socket create error, sockfd: %d", sockfd_);
            exit(SOCKET_ERR);
        }
        lg(Info, "socket create success, sockfd: %d", sockfd_);
        // 2. bind socket
        struct sockaddr_in local;
        bzero(&local, sizeof(local));
        local.sin_family = AF_INET;
        local.sin_port = htons(port_); //需要保证我的端口号是网络字节序列，因为该端口号是要给对方发送的
        local.sin_addr.s_addr = inet_addr(ip_.c_str()); //1. string -> uint32_t 2. uint32_t必须是网络序列的 // ??
        // local.sin_addr.s_addr = htonl(INADDR_ANY);

        if(bind(sockfd_, (const struct sockaddr *)&local, sizeof(local)) < 0)
        {
            lg(Fatal, "bind error, errno: %d, err string: %s", errno, strerror(errno));
            exit(BIND_ERR);
        }
        lg(Info, "bind success, errno: %d, err string: %s", errno, strerror(errno));
    }
    void CheckUser(const struct sockaddr_in &client, const std::string clientip, uint16_t clientport)
    {
        auto iter = online_user_.find(clientip);
        if(iter == online_user_.end())
        {
            online_user_.insert({clientip, client});
            std::cout << "[" << clientip << ":" << clientport << "] add to online user." << std::endl;
        }
    }

    void Broadcast(const std::string &info, const std::string clientip, uint16_t clientport)
    {
        for(const auto &user : online_user_)
        {
            std::string message = "[";
            message += clientip;
            message += ":";
            message += std::to_string(clientport);
            message += "]# ";
            message += info;
            socklen_t len = sizeof(user.second);
            sendto(sockfd_, message.c_str(), message.size(), 0, (struct sockaddr*)(&user.second), len);
        }
    }

    void Run() // 对代码进行分层
    {
        isrunning_ = true;
        char inbuffer[size];
        while(isrunning_)
        {
            struct sockaddr_in client;
            socklen_t len = sizeof(client);
            ssize_t n = recvfrom(sockfd_, inbuffer, sizeof(inbuffer) - 1, 0, (struct sockaddr*)&client, &len);
            if(n < 0)
            {
                lg(Warning, "recvfrom error, errno: %d, err string: %s", errno, strerror(errno));
                continue;
            }
            
            uint16_t clientport = ntohs(client.sin_port);
            std::string clientip = inet_ntoa(client.sin_addr);
            CheckUser(client, clientip, clientport);

            std::string info = inbuffer;
            Broadcast(info,clientip, clientport);
        }
    }
    ~UdpServer()
    {
        if(sockfd_>0) close(sockfd_);
    }
private:
    int sockfd_;     // 网路文件描述符
    std::string ip_; // 任意地址bind 0
    uint16_t port_;  // 表明服务器进程的端口号
    bool isrunning_;
    std::unordered_map<std::string, struct sockaddr_in> online_user_;
};


#if 0
#pragma once

#include <iostream>
#include <string>
#include <strings.h>
#include <cstring>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <functional>
#include "Log.hpp"

// using func_t = std::function<std::string(const std::string&)>;
typedef std::function<std::string(const std::string&)> func_t;

Log lg;

enum{
    SOCKET_ERR=1,
    BIND_ERR
};

uint16_t defaultport=8080;
std::string defaultip="0.0.0.0";
const int size=1024;

class udpServer
{
public:
    udpServer(const uint16_t& port=defaultport,const std::string& ip=defaultip)
        :_sockfd(0)
        ,_port(port)
        ,_ip(ip)
        ,_isrunning(false)
    {}

    //初始化udp服务器
    void Init()
    {
        _sockfd=Create();
        Bind(_sockfd);
    }

    //启动udp服务器
    void Run(func_t func) //运行的服务器需要执行的函数
    {
        _isrunning=true;
        char inbuffer[size];

        while(_isrunning)
        {
            struct sockaddr_in client;
            socklen_t len = sizeof(client);
            ssize_t n = recvfrom(_sockfd, inbuffer, sizeof(inbuffer)-1, 0, (struct sockaddr*)&client, &len);
            if(n < 0)
            {
                lg(Warning, "接收数据错误: %d, 错误信息: %s", errno, strerror(errno));
                continue;
            }
            inbuffer[n] = 0;

            //进行一次数据的处理
            std::string info = inbuffer;
            
            //std::string echo_string="server echo#"+info;
            //std::cout<<echo_string<<std::endl;
            std::string echo_string = func(info);

            sendto(_sockfd, echo_string.c_str(), echo_string.size(), 0, (const sockaddr*)&client, len);
        }
    }

    //创建udp套接字
    int Create()
    {
        //IPV4  UDP
        int sock=socket(AF_INET,SOCK_DGRAM,0);
        if(sock<0)
        {
            lg(Fatal,"udp套接字创建失败，_sockfd:",sock);
            exit(SOCKET_ERR);
        }
        lg(Info,"udp套接字创建成功，_sockfd:",sock);
        return sock;
    }

    //绑定套接字
    void Bind(int sock)
    {
        //创建一个保存本地ip和端口的结构体
        struct sockaddr_in local;
        bzero(&local,sizeof(local));
        local.sin_family=AF_INET;
        local.sin_port=htons(_port); //将端口转换为网络字节序
        local.sin_addr.s_addr=inet_addr(_ip.c_str()); //将字符串转换为网络序列

        //绑定sock套接字
        if(bind(sock,(const struct sockaddr*)&local,sizeof(local))<0)
        {
            lg(Fatal,"绑定udp套接字失败，_sockfd:",sock);
            exit(BIND_ERR);
        }
        lg(Info,"绑定udp套接字成功，_sockfd:",sock);
    }

    ~udpServer()
    {
        if(_sockfd>0) close(_sockfd);
    }

private:
    int _sockfd; //文件描述符套接字
    std::string _ip; //ip地址
    uint16_t _port; //进程的端口号
    bool _isrunning; //是否运行
};
#endif